package cn.qidd.keep.manage.config.job;

import cn.qidd.keep.manage.service.job.SystemJobService;
import cn.qidd.keep.manage.util.TaskConstants;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

/**
 * Created by sunyanping on 2018/10/31
 */
@Component
@DisallowConcurrentExecution
public class SystemJob extends QuartzJobBean {

    @Configuration
    public class SystemJobConfig {

        @Bean
        public JobDetail systemJobDetail() {
            return JobBuilder.newJob(SystemJob.class)
                    .withIdentity(JobKey.jobKey(SystemJob.class.getSimpleName(), TaskConstants.TaskGroup.DISABLE.name()))
                    .withDescription("系统晚间定时任务")
                    .storeDurably(true)
                    .build();
        }

        @Bean
        public Trigger systemJobTrigger() {
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("* */10 * * * ?");
            return TriggerBuilder.newTrigger()
                    .forJob(systemJobDetail())
                    .withIdentity(TriggerKey.triggerKey(SystemJob.class.getSimpleName()))
                    .withPriority(5)
                    .withSchedule(scheduleBuilder)
                    .build();
        }
    }

    @Autowired
    private SystemJobService systemJobService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        systemJobService.resetData();
    }

}